package cn.lutu.adapter.controller;

import cn.lutu.domain.service.SpotService;
import cn.lutu.protocol.dto.*;
import cn.lutu.protocol.vo.HotelVO;
import cn.lutu.protocol.vo.RouteVO;
import cn.lutu.protocol.vo.SpotVO;
import cn.lutu.protocol.vo.VoteVO;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.ResultVO;
import cn.lutu.util.response.StatusCode;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 判断该用户登录状态 是否有权限进行以下操作
 * */
@Slf4j
@Api(tags = "景区产品功能")
@RestController
@RequestMapping("/v1/spots/")
public class  SpotController {
    @Autowired
    SpotService spotService;

    @ApiOperation(value = "添加景区产品功能")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('/product/addNew')")
    public ResultVO insert(@RequestBody SpotDTO spotDTO){
        spotService.insert(spotDTO);
        return ResultVO.ok();
    }
    @ApiOperation(value = "修改景区产品功能")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('/product/update')")
    public ResultVO update(@RequestBody SpotDTO spotDTO){
        spotService.update(spotDTO);
        return ResultVO.ok();
    }
//    @ApiOperation(value = "删除景区产品功能")
//    @PostMapping("/{id}/delete")
//    public ResultVO delete(@PathVariable Long id){
//        spotService.delete(id);
//        return ResultVO.ok();
//    }

    @ApiOperation(value = "景区列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/list")
    public ResultVO select(Integer page, String queryType){
        log.debug("开始处理【景区列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<SpotVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = spotService.selectSpotList(1, Integer.MAX_VALUE);
        } else {
            pageData = spotService.selectSpotList(pageNum);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @ApiOperation(value = "查询景区列表")
    @GetMapping("/listAll")
    public ResultVO selectlist(){
        log.debug("查询景区列表");
        List<SpotVO> spotVO = spotService.selectAllList();
        return ResultVO.ok(spotVO);
    }

    @ApiOperation(value = "首页热门景区")
    @GetMapping("/listIndex")
    public ResultVO selectIndex(){
        log.debug("查询热门景点");
        List<SpotVO> spotVO = spotService.selectIndex();
        return ResultVO.ok(spotVO);
    }


    @ApiOperation(value = "查询景区详情")
    @GetMapping("/{id}/list")
    public ResultVO selectById(@PathVariable Long id){
        log.debug("根据ID查询景区详情数据，参数{}",id);
        SpotVO spotVO = spotService.selectSpotLIstById(id);
        log.debug(" spotVO 参数{}",spotVO);
        return ResultVO.ok(spotVO);
    }

    @ApiOperation(value = "查询门票")
    @GetMapping("/{id}/selectVote")
    public ResultVO selectVoteById(@PathVariable Long id){
        log.debug("根据ID查询门票详情数据，参数：{}",id);
        List<VoteVO> list = spotService.selectVoteById(id);
        return ResultVO.ok(list);

    }
    @ApiOperation("修改景点")
    @PostMapping("/{id:[0-9]+}/updateSpot")
    @PreAuthorize("hasAnyAuthority('/product/update')")
    public ResultVO updateInfoById(@Validated SpotUpdateDTO spotUpdateDTO) {
        log.debug("开始处理【修改景点】的请求，参数：{}", spotUpdateDTO);
        spotService.updateSpotById(spotUpdateDTO);
        return ResultVO.ok();
    }
    @PostMapping("/{id}/delete")
    @ApiOperation("删除景点")
    @PreAuthorize("hasAnyAuthority('/product/delete')")
    public ResultVO deleteAuthorityById(@PathVariable Long id){
        log.info("开始执行删除景点请求");
        spotService.deleteSpotById(id);
        return ResultVO.ok();
    }
    @PostMapping("/new-add")
    @ApiOperation("添加类别")
    @PreAuthorize("hasAnyAuthority('/product/addNew')")
    public ResultVO addSpot(@Valid SpotAddDTO spotAddDTO) {
        log.debug("开始处理【添加景点】的请求，参数：{}", spotAddDTO);
        spotService.addSpot(spotAddDTO);
        return ResultVO.ok();
    }




}
